﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using OpenPowerPoint.Collection;

namespace OpenPowerPointTest.Collections
{
    class NotifierClass : DependencyObject, INotify
    {
        public event NotifyableList<INotify>.PropertyChangedDelegate PropertyChanged;

        private void OnPropertyChanged(SProperty property)
        {
            NotifyableList<INotify>.PropertyChangedDelegate handler = PropertyChanged;
            if (handler != null) handler(this, property);
        }

        //Same with dependency object
        public static readonly DependencyProperty CaptionProperty =
            DependencyProperty.Register("Caption", typeof(string), typeof(NotifierClass), new PropertyMetadata(default(string), (o, e) => ((NotifierClass)o).OnPropertyChangedDepProp(new SProperty(e.Property, e.OldValue, e.NewValue))));

        public string Caption
        {
            get { return (string)GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }

        private bool _avoidEvent;
        private void OnPropertyChangedDepProp(SProperty property)
        {
            if (!_avoidEvent)
            {
                OnPropertyChanged(property);

                _avoidEvent = true;

                if (property.Property == CaptionProperty)
                    Caption = (string)property.NewValue;

                _avoidEvent = false;
            }
        }
    }
}
